<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>AddStudent</title>
    <style>
        .container{
            width: 50%;
            margin: 0 auto;
            text-align: center;
        }
        .title{
            margin: 20px 0;
            font-size: x-large;
        }
        .stu{
            margin-top: 20px;
            border: 1px solid #e9e9e9;
            padding: 55px 0;
            box-shadow: 0 0 100px 30px #D9D9D9;
            border-radius: 15px;
        }
        .stuTable{
            margin: 0 auto;
        }
        .sub{
            margin: 20px 0 0 0;
        }
        .sub .submit{
            margin-right: 10px;
        }
        .sub .back{
            margin-left: 10px;
        }
        #sdept{
            height: 21.3px;
            width: 166px;
        }
    </style>
</head>
<body>
<?php
    /*
     * 修改学生信息
     */
    include "./conn.php";    //连接数据库
    if (isset($_REQUEST['update'])){
        /*
         * 获取表单数据
         */
        $id = $_REQUEST['sno'];
        $name = $_REQUEST['name'];
        $password = $_REQUEST['password'];
        $sex = $_REQUEST['sex'];
        $birthday = $_REQUEST['birthday'];
        $sdept = $_REQUEST['sdept'];
        $phone = $_REQUEST['phone'];
        $email = $_REQUEST['email'];

        /*
         * 上传图片
         */
        if($_FILES["image"]["error"]>0){
            $fileName = $_REQUEST['img'];
//            echo "上传图片错误：: " .$_FILES["image"]["error"] . "<br>";   //上传图片出错时提示
        }else{
            $fileName = $_FILES['image']['name'];
            $fileName = iconv('UTF-8','GB2312',$fileName);  //设置文件编码
            date_default_timezone_set("Asia/shanghai");   //设置时区
            $mytime = date("YmdHis");   //设置时间戳日期转换
            $tagarr = explode(".",$_FILES['image']['name']);   //拆分文件名
            $myextension = end($tagarr);    //获取扩展名
            $target_dir = "./images/";   //保存到本地目录
            $target_file = $target_dir.$mytime.'.'.$myextension;
            $fileName = $mytime.'.'.$myextension;    //拼接后的文件名
            $temSrc = $_FILES['image']['tmp_name'];    //文件的临时存放目录
            move_uploaded_file($temSrc, $target_file);   //将文件上传到目标目录
        }
        //sql语句
        $sql = "update stu set sname='$name', password='$password',
                sex='$sex',birthday= '$birthday', sdept='$sdept', phone=$phone,email='$email',images='$fileName' where sno=$id";
        $result  = mysqli_query($con,$sql);    //获取结果
        if($result) echo "<script language='JavaScript'>alert('修改成功')</script>";    //修改成功提示
        else echo "修改失败！";    //修改失败提示
    }
    /*
     * 显示数据
     */
    $id  = $_REQUEST['id'];    //获取id
    $sql = "select * from stu where sno=$id";
    $result = mysqli_query($con,$sql);
    $rows = mysqli_fetch_array($result);
?>
<div class="container">
    <div class="title">修改学生信息</div>
    <form action="" method="post" name="addStu" class="stu" enctype="multipart/form-data">
        <table class="stuTable">
            <tr>
                <td><span>学号:</span></td>
                <td><input type="text" name="sno" value="<?php echo $rows['sno']?>"><span></span></td>
            </tr>
            <tr>
                <td><span>姓名:</span></td>
                <td><input type="text" name="name" value="<?php echo $rows['sname']?>"><span></span></td>
            </tr>
            <tr>
                <td><span>密码:</span></td>
                <td><input type="password" name="password" value="<?php echo $rows['password']?>"><span></span></td>
            </tr>
            <tr>
                <td><span>性别:</span></td>
                <td>
                    <?php
                    if($rows[3]=="男")
                        echo "男<input type=\"radio\" name=\"sex\" value=\"男\" checked>
                        女<input type=\"radio\" name=\"sex\" value=\"女\"> </td>";
                    else
                        echo "男<input type=\"radio\" name=\"sex\" value=\"男\" >
                    女<input type=\"radio\" name=\"sex\" value=\"女\" checked> </td>"
                    ?>
            </tr>
            <tr>
                <td><span>生日:</span></td>
                <td><input type="text" name="birthday" value="<?php echo $rows['birthday']?>"><span></span></td>
            </tr>
            <tr>
                <td><span>部门:</span></td>
                <td><select name="sdept" id="sdept" >
                        <option value="请选择部门" <?php if($rows[5]=="") echo "selected"?>>请选择部门</option>
                        <option value="信息工程学院" <?php if($rows[5]=="信息工程学院") echo "selected"?>>信息工程学院</option>
                        <option value="彩虹鱼学院" <?php if($rows[5]=="彩虹鱼学院") echo "selected"?>>彩虹鱼学院</option>
                        <option value="汽车学院" <?php if($rows[5]=="汽车学院") echo "selected"?>>汽车学院</option>
                        <option value="达利女装学院" <?php if($rows[5]=="达利女装学院") echo "selected"?>>达利女装学院</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><span>手机:</span></td>
                <td><input type="text" name="phone" value="<?php echo $rows['phone']?>"><span></span></td>
            </tr>
            <tr>
                <td><span>Email:</span></td>
                <td><input type="text" name="email" value="<?php echo $rows['email']?>"><span></span></td>
            </tr>
            <tr>
                <td><span>图片:</span></td>
                <td><input style="width: 166.3px" type="file" name="image" ></td>
                <input type="hidden" name="img" value="<?php echo $rows['images']?>">
            </tr>
            <tr>
                <td><span>预览：</span></td>
                <td><img width="60px" src="./images/<?php echo $rows['images'] ?>" alt=""></td>
                <td><?php echo $rows['images'] ?></td>
            </tr>
        </table>
        <div class="sub">
            <input type="submit" class="submit" name="update" value="修改">
            <input type="button" class="back" onclick="window.location.href='stuList.php'" value="返回">
        </div>
    </form>
</div>
</body>
</html>